<?php

namespace App\Http\Controllers\Admin\Office;

use App\Http\Controllers\Admin\CommonController;
use App\Models\Office\Office;
use App\Requests\Office\StoreRequest;
use App\Exceptions\ExceptionSms;
use Carbon\Carbon;
use App\Helpers\MoneyHelper;

/**
 * 办公管理
 * Class OfficeController
 * @package App\Http\Controllers\Admin\Office
 */
class OfficeController extends CommonController
{
    protected $model = Office::class;

    public function index()
    {
        $search_forms = Office::getForms(['name']);

        $cols = [[
            ['field' => 'name', 'title' => '名称'],
            ['field' => 'thumbnail_format', 'title' => '封面', 'escape' => false],
            ['field' => 'name', 'title' => '地址'],
            ['field' => 'action', 'title' => '操作', 'templet' => '#list-bar']
        ]];

        $permission_code = 'office-query';

        $uri = '/admin/office/list?code=' . $permission_code;

        $top_btns = [
            ['code' => 'office-add']
        ];

        $row_btns = [
            ['code' => 'office-edit'],
            ['code' => 'office-delete'],
        ];

        return view('components.list', compact('search_forms', 'cols', 'permission_code', 'uri', 'top_btns', 'row_btns'));
    }

    public function dataList()
    {
        $list = Office::bgBaseQuery($this->input())->paginate($this->limit);
        foreach ($list as $resource) {
            $resource->setAppends(['faddress']);

            $resource->thumbnail_format = $resource->formatImage($resource->thumbnail);
        }
        return ExceptionSms::homeList($list->items(), $list->total());
    }

    public function editAdd()
    {
        $id   = $this->input('id') ?? 0;
        $data = $id ? Office::getOneById($id) : null;

        $save_forms = Office::getForms(['name', 'thumbnail', 'images', 'area_ids', 'address', 'desc', 'content'], 'save');
        if ($data) {
            $save_forms['content']['width'] = '80%';
            $save_forms['area_ids']['value']  = array_values(array_filter([$data->province_id, $data->city_id, $data->district_id]));
            $save_forms['thumbnail']['value'] = $data->thumbnail;
            $save_forms['images']['value']    = $data->images;
        }
        $permission_code = $id ? 'office-edit-save' : 'office-add-save';

        return view('components.save_form', compact('data', 'save_forms', 'permission_code'));
    }

    public function dataSave(StoreRequest $request)
    {
        $id = $request->get('id') ?? "";
        if ($id) $data = Office::getOneById($id);
        else $data = new Office();
        $data->setBgArea($request);
        $data->name      = $request->get("name");
        $data->thumbnail = $request->get("thumbnail");
        $data->images    = explode(',', $request->get('images'));
        $data->address   = $request->get("address");
        $data->desc      = $request->get("desc");
        $data->content   = $request->get("content") ?? "";
        $data->save();
        return ExceptionSms::returnSuccess();
    }
}
